package com.controller;

import com.domain.IssueComment;
import com.domain.IssueCommentResponse;
import com.domain.IssueCommentResponseGrandson;
import com.mapper.helping.IssueCommentMapper;
import com.service.IIssueCommentResponseGrandsonService;
import com.service.IIssueCommentResponseService;
import com.util.UserUtil;
import com.verify.UserVerify;
import com.verify.WeiXinVerify;
import com.websocketOfNotice.msgAnno.Notice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import living.com.result.ApiResult;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 大忽悠
 * @create 2022/2/12 19:06
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "孙子评论请求处理")
public class GrantSonCommentController {
    private final IIssueCommentResponseGrandsonService iIssueCommentResponseGrandsonService;
    private final IIssueCommentResponseService iIssueCommentResponseService;
    private final IssueCommentMapper issueCommentMapper;

    //id_index默认为儿子评论id
    @Notice(id_index = 0,notice_type = "COMMENT_ON_SON_OR_GRANTSON_COMMENT",grant_son_comment_id_index = 3)
    @UserVerify
    @WeiXinVerify(paramIndex = 2,paramName = "String",weiXinVerifyType = 5)
    @ApiOperation("在指定儿子或孙子评论下进行评论")
    @PostMapping("/grantSonComment")
    @Transactional(value = "primaryTransactionManager")
    public ApiResult<Integer> postGrantSonComment(
            @ApiParam("儿子评论id")@RequestParam Integer son_comment_id,
            @ApiParam("孙子评论回复类型,回复儿子传入0,回复非匿名孙子传入孙子评论对应用户的id,回复匿名孙子,传入-1")@RequestParam Integer to_user_id,
                                             @ApiParam("内容")@RequestParam String content,
            @ApiParam("当回复的是非匿名孙子评论的时候,传入被回复的孙子评论的id")@RequestParam(defaultValue = "-1") Integer grant_son_comment_id)
    {
        //字数限制判断
        Assert.isTrue(content.length()>0&&content.length()<=1000,"评论字数在1到1000字之间");
        //保存孙子评论
        IssueCommentResponseGrandson issueCommentResponseGrandson = IssueCommentResponseGrandson.builder()
                //father_comment_id一定是儿子评论id
                .fatherCommentId(son_comment_id)
                .content(content).fromUserId(UserUtil.getUserId()).build();
        //需要知道当前孙子评论是对谁进行回复的
        issueCommentResponseGrandson.setToUserId(to_user_id);
        //如果匿名答主自己进行评论的---当前评论是匿名版主发布的
        IssueComment issueComment = issueCommentMapper.selectById(iIssueCommentResponseService.getById(son_comment_id).getFatherCommentId());
        if(issueComment.getIsAnony()==1&&issueComment.getUserId().equals(UserUtil.getUserId()))
        {
            issueCommentResponseGrandson.setFromUserId(0);
        }
        iIssueCommentResponseGrandsonService.save(issueCommentResponseGrandson);
        //对应儿子的回复数加一
        IssueCommentResponse issueCommentResponse = iIssueCommentResponseService.getById(son_comment_id);
        issueCommentResponse.setResponseSum(issueCommentResponse.getResponseSum()+1);
        iIssueCommentResponseService.updateById(issueCommentResponse);
        return ApiResult.success(issueCommentResponseGrandson.getId());
    }

    @UserVerify
    @ApiOperation("逻辑删除指定孙子评论")
    @DeleteMapping("logicDelGrantSon")
    public ApiResult<Boolean> logicDelTargetGrantSonComment(@ApiParam("回答id")@RequestParam Integer comment_id,@ApiParam("孙子评论id")@RequestParam Integer grantSon_comment_id)
    {
        Boolean delById = iIssueCommentResponseGrandsonService.logicDelById(grantSon_comment_id);
        return ApiResult.success(delById);
    }

    @ApiOperation("获取指定儿子评论下的所有孙子评论")
    @GetMapping("getGrantSons")
    public ApiResult<Map<String,Object>> getSonCommentUnderTargetFatherComment(
            @ApiParam("儿子评论id")@RequestParam Integer father_comment_id,
            @ApiParam("起始页")@RequestParam Integer startPage,
            @ApiParam("一页多少条数据")@RequestParam Integer pageSize)

    {
        return ApiResult.success(iIssueCommentResponseGrandsonService.getSonCommentUnderTargetFatherComment(father_comment_id,startPage,pageSize));
    }

}
